Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = 0;
    if (options.id > 0) {
      id = options.id;
      this.setData({
        currentTab: id
      })
    } else {
      id = this.data.currentTab;
    }
    console.log(this.data.currentTab);
    var that = this;
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "action": "GetOrderListByStatus",
        "userid": wx.getStorageSync('userid'),
        "status": id
      },
      dataType: "json",
      success: function(res) {
        console.log(res.data);
        that.setData({
          list: res.data
        })
      }
    })
  },
  //点击切换
  clickTab: function(e) {
    var that = this;
    var status = e.target.dataset.current;
    if (this.data.currentTab == status) {
      return;
    } else {
      this.setData({
        currentTab: status
      })
    }
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "action": "GetOrderListByStatus",
        "userid": wx.getStorageSync('userid'),
        "status": that.data.currentTab
      },
      dataType: "json",
      success: function(res) {
        that.setData({
          list: res.data
        })
      }
    })
  },
  submitpay: function(e) {
    var marking = e.currentTarget.dataset.marking;
    console.log(marking);
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'action': 'GetPayJson',
        'openId': marking,
        'userid': wx.getStorageSync('userid')
      },
      dataType: 'json',
      success: function(res) {
        console.log(res.data)
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success: function(json) {
            wx.request({
              url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
              method: 'Post',
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              data: {
                'action': 'UserPayOrder',
                'openId': marking,
              },
              dataType: "json",
              success: function(res) {
                console.log(res.data);
              }
            })
          },
          fail: function(res) {
            wx.showModal({
              title: '提示',
              content: '已取消支付',
              showCancel: false,
              success: function() {
                wx.navigateTo({
                  url: '../orderlist/orderlist',
                })
              }
            })
          }
        })
      }
    })
  },
  CancelOrder: function(e) {
    var marking = e.currentTarget.dataset.marking;
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要取消该订单吗',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            data: {
              'action': 'CancelOrder',
              'orderId': marking,
              'userid': wx.getStorageSync('userid')
            },
            dataType: 'json',
            success: function(res) {
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: '取消订单成功',
                  showCancel: false,
                  success: function() {
                    that.onShow();
                  }
                })
              }
            }
          })
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "action": "GetOrderListByStatus",
        "userid": wx.getStorageSync('userid'),
        "status": that.data.currentTab
      },
      dataType: "json",
      success: function(res) {
        console.log(res.data);
        that.setData({
          list: res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})